package com.framework.modules.live.service.impl;

import com.framework.common.utils.DateUtils;
import com.framework.modules.dataview.service.ViewUserInfoLiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.Query;

import com.framework.modules.live.dao.LiveRoomDao;
import com.framework.modules.live.entity.LiveRoomEntity;
import com.framework.modules.live.service.LiveRoomService;
import org.springframework.transaction.annotation.Transactional;


@Service("liveRoomService")
public class LiveRoomServiceImpl extends ServiceImpl<LiveRoomDao, LiveRoomEntity> implements LiveRoomService {


    @Autowired
    private ViewUserInfoLiveService viewUserInfoLiveService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<LiveRoomEntity> page = this.selectPage(
                new Query<LiveRoomEntity>(params).getPage(),
                new EntityWrapper<LiveRoomEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public Integer updateStreamEndLiveRoom(Integer seconds, Integer stopTime,Integer state, Integer uid) {
        return baseMapper.updateStreamEndLiveRoom(seconds, stopTime,state, uid);
    }

    @Override
    public Object findLiveInfo(Integer uid) {
        LiveRoomEntity liveRoomEntity = this.baseMapper.selectById(uid);
        if (liveRoomEntity == null) {
            liveRoomEntity = new LiveRoomEntity();
            liveRoomEntity.setUid(uid);
            liveRoomEntity.setState(0);
            liveRoomEntity.setRank(0);
            liveRoomEntity.setGiftCount(0);
            liveRoomEntity.setTotalSeconds(0);
            liveRoomEntity.setPopCount(0);
            liveRoomEntity.setStartDate(DateUtils.getSecondTimestamp(new Date()));
            this.baseMapper.insert(liveRoomEntity);
        }
        return viewUserInfoLiveService.getViewUserInfoLive(uid.longValue());
    }
}
